﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using Context;
namespace WCF_ListenMusic.Services.Module_Playlist.User
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "PlaylistService" in code, svc and config file together.
    [ServiceBehavior(TransactionAutoCompleteOnSessionClose=true)]
    public class PlaylistService : IPlaylistService
    {
        Connect_DBDataContext context = new Connect_DBDataContext();
        #region IPlaylistService Members

        public List<Entities.Playlist> getPlaylistByMemberID(string UserName)
        {
            try
            {
                if (UserName == null)
                {
                    throw new FaultException("UserName trống");
                }
                else
                {
                    var q = from p in context.Playlists
                            where p.UserName.Equals(UserName)
                            select p;
                    return q.ToList();         
                   
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region IPlaylistService Members


        public bool InsertMusic_Into_Favorites(Favorite f)
        {
            try
            {
                f.FavoritesID = getMax_FavoritesID() +1;
                context.Favorites.InsertOnSubmit(f);
                context.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region IPlaylistService Members


        public int getMax_FavoritesID()
        {

            try
            {

                var q = (from c in context.Favorites
                         select (int?)c.FavoritesID).Max() ?? 0;
                return q;
                        
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
           
        }

        #endregion

        #region IPlaylistService Members


        public List<FavoritePlayList> getSongInPlaylist_ByMemberID(string UserName)
        {
            try
            {
                if (UserName == null)
                {
                    throw new FaultException("UserName Trống");
                }
                else
                {
                    var q = from p in context.FavoritePlayLists
                            where p.UserName.Equals(UserName)
                            select p;
                    return q.ToList();
                }

            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region IPlaylistService Members


        public bool InsertPlaylist(Playlist p)
        {
            try
            {
                DateTime d = DateTime.Now;
                p.PlaylistID = getMax_PlaylistID() + 1;
                p.DateCreate = d.Date;
                context.Playlists.InsertOnSubmit(p);
                context.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region IPlaylistService Members


        public int getMax_PlaylistID()
        {
            try
            {

                var q = (from p in context.Playlists
                         select (int?)p.PlaylistID).Max() ?? 0;
                return q;

            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
           
        }

        #endregion

        #region IPlaylistService Members


        public List<FavoritePlayList> getSongInSpecificPlaylist(string UserName, int playlistID)
        {
            try
            {
                if (UserName == null || playlistID < 1)
                {
                    throw new FaultException("playlistID nhỏ hơn 1 Hoặc UserName Trống");
                }
                else
                {
                    var q = from p in context.FavoritePlayLists
                            where p.UserName.Equals(UserName) && p.PlaylistID.Equals(playlistID)
                            select p;
                    return q.ToList();

                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region Edit Playlist


        public bool EditPlaylist(Playlist newpl)
        {
            try
            {
                Playlist oldpl = (from p in context.Playlists
                                  where p.PlaylistID.Equals(newpl.PlaylistID) 
                                  select p).SingleOrDefault();
                oldpl.PlaylistName = newpl.PlaylistName;
                oldpl._Status = newpl._Status;
                if (newpl.Picture != "" && newpl.Picture != null)
                {
                    oldpl.Picture = newpl.Picture;
                }
                try
                {
                    context.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                    throw new FaultException("Update Thất Bại: " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region Get Playlist By PlaylistID


        public Playlist getPlaylist_ByPlaylistID(int playlistID)
        {
            try
            {
                if (playlistID <= 0)
                {
                    throw new FaultException("playlistID nhỏ hơn không.");
                }
                else
                {
                    var q = from p in context.Playlists
                            where p.PlaylistID.Equals(playlistID)
                            select p;
                    return q.SingleOrDefault();
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }
        

        #endregion

        #region Edit PlayList Picture

        [OperationBehavior(TransactionAutoComplete=true,TransactionScopeRequired=true)]
        public bool EditPlayListPicture(int plid, string imgurl)
        {
            try
            {
                Playlist oldpl = (from p in context.Playlists
                                  where p.PlaylistID.Equals(plid)
                                  select p).SingleOrDefault();
                oldpl.Picture = imgurl;
                try
                {
                    context.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                    throw new FaultException("Update Thất Bại: " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        #endregion

        #region Delete Song In Playtlist


        public bool DeleteSongInPlaylist(int fid)
        {
            if (fid < 1)
            {
                throw new FaultException("Favorites ID nhỏ hơn 1");
            }
            else
            {
                try
                {
                    var q = (from p in context.Favorites
                             where p.FavoritesID.Equals(fid)
                             select p).SingleOrDefault();
                    if (q == null)
                    {
                        return false;
                    }
                    try
                    {
                        context.Favorites.DeleteOnSubmit(q);
                        context.SubmitChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        return false;
                        throw new FaultException("Delete Thất Bại: " + ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    return false;
                    throw new FaultException(ex.Message);
                }

            }   
        }

        #endregion

        #region Delete Playlist


        public bool DeletePlaylist(int plid)
        {

            if (plid < 1)
            {
                throw new FaultException("Playlist ID nhỏ hơn 1");
            }
            else
            {
                try
                {
                    var q = (from p in context.Playlists
                             where p.PlaylistID.Equals(plid)
                             select p).SingleOrDefault();
                    if (q == null)
                    {
                        return false;
                    }
                    try
                    {
                        context.Playlists.DeleteOnSubmit(q);
                        context.SubmitChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        return false;
                        throw new FaultException("Delete Thất Bại: " + ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    return false;
                    throw new FaultException(ex.Message);
                }

            }
        }

        #endregion
    }
}
